package com.qs.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.qs.annotation.OperationLog;
import com.qs.pojo.dto.AdminDto;
import com.qs.pojo.dto.CategoryDto;
import com.qs.pojo.entity.Admin;
import com.qs.pojo.entity.Category;
import com.qs.response.Response;
import com.qs.service.ICategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-11-12
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class CategoryController {

    private final ICategoryService categoryService;

    /**
     * 获取商品分类数据
     * @param
     * @param
     * @param name
     * @return
     */
    @GetMapping("/list")
    public Response list(String name) {
        log.info("获取商品分类数据请求进来--------------------");
        List<Tree<String>> categoryList = categoryService.getList(name);
        return Response.success(categoryList);
    }

    /**
     * 前台获取用户点击某分类下的分类全称
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/byName/{categoryName}")
    public Response getCategorys(@PathVariable("categoryName") String categoryName) {
        log.info("前台获取用户点击某分类下的分类全称请求进来--------------------");
        String category = categoryService.getCategorys(categoryName);
        return Response.success(category);
    }

    /**
     * 新增商品分类
     * @param category
     * @return
     */
    @PostMapping("/save")
    @OperationLog(operModul = "分类管理-新增分类",operDesc = "管理员新增商品分类")
    public Response saveAdmin(@RequestBody Category category) {
        log.info("新增商品分类请求进来--------------------");
        categoryService.saveCategory(category);
        return Response.success();
    }

    /**
     * 修改商品分类
     * @param
     * @param
     * @param
     * @return
     */
    @PutMapping("/update")
    @OperationLog(operModul = "分类管理-修改分类",operDesc = "管理员修改商品分类")
    public Response update(@RequestBody CategoryDto categoryDto) {
        log.info("修改商品分类请求进来--------------------");
        categoryService.updateByKey(categoryDto);
        return Response.success();
    }

    /**
     * 商品分类数据回显
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/getById/{id}")
    public Response echo(@PathVariable Integer id) {
        log.info("商品分类数据回显请求进来--------------------");
        CategoryDto categoryDto = categoryService.echo(id);
        return Response.success(categoryDto);
    }

    /**
     * 删除商品分类
     * @param
     * @param
     * @param
     * @return
     */
    @DeleteMapping("/remove/{ids}")
    @OperationLog(operModul = "分类管理-删除分类",operDesc = "管理员删除商品分类")
    public Response delete(@PathVariable List<Integer> ids) {
        log.info("删除商品分类请求进来--------------------");
        categoryService.deleteBatchByIds(ids);
        return Response.success();
    }
}
